package Controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.DONDATHANG_MOD;
import Model.GIOHANG_MOD;
import Model.THONGTINNGUOINHAN_MOD;
import object.CTDONDATHANG_OBJ;
import object.DONDATHANG_OBJ;
import object.KHACHHANG_OBJ;
import object.MONHANG_OBJ;
import object.THONGTINNGUOINHAN_OBJ;

/**
 * Servlet implementation class DONDATHANG
 */
@WebServlet("/DONDATHANG_CTR")
public class DONDATHANG_CTR extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DONDATHANG_CTR() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DONDATHANG_MOD dondathang_mod=new DONDATHANG_MOD();
    	String MaDH=request.getParameter("madh");
		if(MaDH!=null)
		{
			dondathang_mod.HuyDonhang(MaDH);
			response.sendRedirect("admin/Quanlydonhang.jsp");
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String searchby=request.getParameter("searchby");
		String keyword=request.getParameter("search");
		if(searchby!=null)
		{
			response.sendRedirect("admin/Quanlydonhang.jsp?searchby="+searchby+"&search="+keyword);
		}
		else
		{
			THONGTINNGUOINHAN_OBJ thongtin=new THONGTINNGUOINHAN_OBJ();//Dung de luu thong tin nguoi nhan
			DONDATHANG_MOD dondathang_mod=new DONDATHANG_MOD();
			HttpSession session=request.getSession(true);
			KHACHHANG_OBJ khachhang=(KHACHHANG_OBJ) session.getAttribute("Khachhang");//Lay thong tin khach hang
			GIOHANG_MOD giohang=(GIOHANG_MOD)session.getAttribute("giohang");//Lay thong tin gio hang
			//Thiet lap thong tin nguoi nhan
//			String MaDH=dondathang_mod.TaomaDONHANG();//Tao ma don hang
//			thongtin.setMaDH(MaDH);
			thongtin.setHoten(request.getParameter("hoten"));
			thongtin.setDiachi(request.getParameter("diachi"));
			thongtin.setDienthoai(request.getParameter("dienthoai"));
			thongtin.setPthucnhanhang(request.getParameter("phuongthucgiaohang"));
			thongtin.setHthucthanhtoan(request.getParameter("hinhthucthanhtoan"));
			thongtin.setYeucaukhac(request.getParameter("yeucaukhac"));
			session.setAttribute("Thongtinnguoinhan",thongtin);//Luu thong tin nguoi nhan vo session
			String MaDH="";
			try {
				MaDH = dondathang_mod.ThemDonhang(giohang, khachhang, thongtin);//Thêm đơn hàng đồng thời lấy mã đơn hàng vừa thêm
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}//Gọi hàm thêm đơn hàng
			session.setAttribute("MaDH", MaDH);//Lua ma don hang vua tao vo session
			response.sendRedirect("thongtindonhang.jsp");
		}
	}
	

}
